package eu.liquify.ui {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	public class MultiTabBox extends Box implements ISimulatable {
		private var tabs:HBox;
		private var tabBox:VBox;
		private var contentPanel:ScrollablePanel;
		private var allItems:Vector.<DisplayObject> = new Vector.<DisplayObject>();
		private var tabMap:Dictionary = new Dictionary();
		private var setSelected:Boolean;
		public function MultiTabBox() {
			fitToGrid = false;
			
			removeRawChild(content);
			
			tabBox = new VBox();
			tabBox.addChildVector(new <DisplayObject>[
				tabs = new HBox( { fixedSize: false, fitToGrid: false, spacing: 0 } ),
				contentPanel = new ScrollablePanel( { background: false, padding: 0, fixedSize: true } )
			]);
			
			//tabBox.addChild(content);
			contentPanel.addChild(content);
			container = tabBox;
			addRawChild(container);
		}
		
		public function simulateEvent(e:Event):void {
			var m:MouseEvent = e as MouseEvent;
			if (m) {
				var dirty:Boolean = false;
				var prev:Boolean;
				for (var i:int = 0; i < tabs.numChildren; i++) {
					var tab:MultiTabButton = tabs.getChildAt(i) as MultiTabButton;
					switch (e.type) {
						case MouseEvent.MOUSE_DOWN:
							prev = tab.selected;
							tab.selected = tab.getRect(this).contains(m.localX, m.localY);
							if (prev != tab.selected) dirty = true;
							break;
						case MouseEvent.MOUSE_MOVE:
							if (tab.getRect(this).contains(m.localX, m.localY)) {
								if (!tab.selected) dirty = true;
								tab.selected = true;
							}
							break;
					}
					//tab.dispatchEvent(e);
				}
				if (dirty) updateVisible();
			}
		}
		
		override public function resize():void {
			super.resize();
			
			contentPanel.width = width;
			contentPanel.height = height;
			contentPanel.resize();
		}
		
		override protected function reposition():void {
			super.reposition();
			if (!fixedWidth) {
				if (width < tabs.width) {
					width = tabs.width;
					updateLabel();
				}
			}
			if (!fixedHeight) height += tabBox.spacing+tabs.height;
		}
		
		override public function addChild(child:DisplayObject):DisplayObject {
			super.addChild(child);
			
			allItems.push(child);
			
			var tab:MultiTabButton = new MultiTabButton();
			tab.label = getLabel(child);
			tab.name = tab.label;
			tabMap[child] = tab;
			tab.addEventListener(MouseEvent.MOUSE_DOWN, tabDown);
			tabs.addChild(tab);
			
			redrawTabs();
			
			return child;
		}
		override public function removeChild(child:DisplayObject):DisplayObject {
			if (items.indexOf(child) > -1) super.removeChild(child);
			
			allItems.splice(allItems.indexOf(child), 1);
			
			var tab:MultiTabButton = tabMap[child];
			tab.removeEventListener(MouseEvent.MOUSE_DOWN, tabDown);
			tabs.removeChild(tab);
			delete tabMap[child];
			redrawTabs();
			
			contentPanel.resize();
			
			return child;
		}
		
		private function getLabel(child:DisplayObject):String {
			if (child is UIItem) return (child as UIItem).label;
			return child.name;
		}
		
		private function redrawTabs():void {
			for (var i:int = 0; i < tabs.numChildren; i++) {
				var tab:MultiTabButton = tabs.getChildAt(i) as MultiTabButton;
				tab.style = 
					tabs.numChildren == 1 ?			MultiTabButton.SINGLE : 
					(i == 0 ?						MultiTabButton.LEFT :
					(i == tabs.numChildren-1 ?		MultiTabButton.RIGHT :
													MultiTabButton.MIDDLE
				));
			}
			tabs.resize();
		}
		
		private function tabDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, tabUp);
			var tabClicked:Button = e.currentTarget as Button;
			if (!e.shiftKey && !e.ctrlKey) {
				for (var i:int = 0; i < tabs.numChildren; i++) {
					var tab:Button = tabs.getChildAt(i) as Button;
					tab.selected = false;
				}
				setSelected = true;
			} else {
				setSelected = !tabClicked.selected;
			}
			tabClicked.selected = setSelected;
			addTabListeners();
			updateVisible();
		}
		private function tabUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, tabUp);
			removeTabListeners();
		}
		
		private function addTabListeners():void {
			for (var i:int = 0; i < tabs.numChildren; i++) {
				var tab:Button = tabs.getChildAt(i) as Button;
				tab.addEventListener(MouseEvent.ROLL_OVER, tabOver);
			}
		}
		private function removeTabListeners():void {
			for (var i:int = 0; i < tabs.numChildren; i++) {
				var tab:Button = tabs.getChildAt(i) as Button;
				tab.removeEventListener(MouseEvent.ROLL_OVER, tabOver);
			}
		}
		private function tabOver(e:MouseEvent):void {
			var tab:Button = e.currentTarget as Button;
			tab.selected = setSelected;
			updateVisible();
		}
		
		private function updateVisible():void {
			var item:DisplayObject;
			var i:int;
			for (i = 0; i < allItems.length; i++) {
				item = allItems[i];
				if (content.contains(item)) content.removeChild(item);
			}
			items.length = 0;
			for (i = 0; i < allItems.length; i++) {
				item = allItems[i];
				var tab:Button = tabs.getChildByName(getLabel(item)) as Button;
				if (tab.selected) {
					content.addChild(item);
					items.push(item);
				}
			}
			contentPanel.constrain();
			//reposition();
			resize();
			contentPanel.resize();
		}
		
		/*
		private function tabClick(tab:Button):void {
			var child:DisplayObject;
			if (tab.selected) {
				child = hiddenItems.getChildByName(tab.name);
				hiddenItems.removeChild(child);
				super.addChild(child);
			} else {
				child = contents.getChildByName(tab.name);
				super.removeChild(child);
				hiddenItems.addChild(child);
			}
		}
		*/
		
	}
}